package com.example.abe.diandi.trends.activity;

import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.BindView;
import com.example.OnClick;
import com.example.abe.diandi.R;
import com.example.abe.diandi.base.BaseActivity;
import com.example.abe.diandi.main.AppConfig;
import com.example.abe.diandi.nohttp.ApiUtil;
import com.example.abe.diandi.nohttp.CallServer;
import com.example.abe.diandi.nohttp.HttpListener;
import com.example.abe.diandi.trends.fragment.TrendsShowImageFragment;
import com.example.abe.diandi.trends.fragment.TrendsShowTextFragment;
import com.example.abe.diandi.trends.model.ImageTextModel;
import com.example.abe.diandi.trends.model.TrendModel;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.yanzhenjie.nohttp.NoHttp;
import com.yanzhenjie.nohttp.RequestMethod;
import com.yanzhenjie.nohttp.rest.Request;
import com.yanzhenjie.nohttp.rest.Response;

import java.util.ArrayList;

import example.abe.com.framework.util.TimeUtil;
import example.abe.com.framework.util.ToastUtil;

import static com.example.abe.diandi.nohttp.ApiUtil.METHOD;
import static com.example.abe.diandi.nohttp.ApiUtil.M_TREND_COLLECT_TREND;
import static com.example.abe.diandi.nohttp.ApiUtil.M_TREND_DELETE_TREND;
import static com.example.abe.diandi.nohttp.ApiUtil.SERVICE;
import static com.example.abe.diandi.nohttp.ApiUtil.S_TREND;

public class TrendsShowActivity extends BaseActivity {

    public static final String NOTE_MODEL_DATA = "note_model_data";

    @BindView(R.id.act_trends_show_tv_time)
    protected TextView mTvTime;

    @BindView(R.id.act_trends_show_tv_title)
    protected TextView mTvTitle;

    @BindView(R.id.act_trends_show_iv_feel)
    protected ImageView mIvFeel;

    @BindView(R.id.act_trends_show_fl_content)
    protected FrameLayout mFlContent;

    private TrendModel mTrendModel;

    private TrendsShowTextFragment mTextFragment;
    private TrendsShowImageFragment mImageFragment;

    @Override
    public int getLayoutID() {
        return R.layout.activity_trends_show;
    }

    @Override
    public void initData() {
        mTrendModel = getIntent().getParcelableExtra(NOTE_MODEL_DATA);
    }

    @Override
    public void initView() {
        mTvTime.setText(TimeUtil.getStrCurrentCH("yyyy年MM月dd日"));
        mTvTitle.setText(mTrendModel.getTitle());

        if (mTrendModel.getType() == TrendModel.TYPE_TEXT){
            mTextFragment = TrendsShowTextFragment.instance(mTrendModel.getContent());
            replaceFragment(mFlContent, mTextFragment);
        }else if (mTrendModel.getType() == TrendModel.TYPE_IMAGE){
            ArrayList<ImageTextModel> list = new ArrayList<>(mTrendModel.getListImageTextIds());
            mImageFragment = TrendsShowImageFragment.instance(list, mTrendModel.getType());
            replaceFragment(mFlContent, mImageFragment);
        }else if (mTrendModel.getType() == TrendModel.TYPE_IMAGE_TEXT){
            ArrayList<ImageTextModel> list = new ArrayList<>(mTrendModel.getListImageTextIds());
            mImageFragment = TrendsShowImageFragment.instance(list, mTrendModel.getType());
            replaceFragment(mFlContent, mImageFragment);
        }
    }

    @Override
    public void onSideSlipEvent(){
        finish();
    }

    @OnClick(R.id.act_trends_show_tv_back)
    protected void onBackClick(){
        finish();
    }

    @OnClick(R.id.act_trends_show_fl_collect)
    protected void onTrendsCollectClick(){
        Request<String> request = NoHttp.createStringRequest(ApiUtil.URL, RequestMethod.POST);
        request.add(SERVICE, S_TREND)
                .add(METHOD, M_TREND_COLLECT_TREND)
                .add("trendId", mTrendModel.getId())
                .add("isCollect", !mTrendModel.isCollect());
        CallServer.getInstance().add(this, request, new HttpListener<String>() {

            @Override
            public void onSucceed(int what, Response<String> response) {
                JsonObject jsonObj = new JsonParser().parse(response.get()).getAsJsonObject();
                Boolean isSuccess = jsonObj.get("success").getAsBoolean();
                if (isSuccess) {
                    JsonObject data = jsonObj.getAsJsonObject("data");
                    mTrendModel = new Gson().fromJson(data, TrendModel.class);
                    ToastUtil.makeText(mTrendModel.isCollect()?"收藏成功": "取消收藏成功");
                } else {
                    ToastUtil.makeText("收藏失败");
                }
            }

            @Override
            public void onFailed(int what, Response<String> response) {
                ToastUtil.makeText("收藏失败");
            }
        });
    }

    @OnClick(R.id.act_trends_show_fl_trash)
    protected void onTrendsTrashClick(){
        Request<String> request = NoHttp.createStringRequest(ApiUtil.URL, RequestMethod.POST);
        request.add(SERVICE, S_TREND)
                .add(METHOD, M_TREND_DELETE_TREND)
                .add("userId", AppConfig.getUserModel().getId())
                .add("categoryId", mTrendModel.getCategory_id())
                .add("trendId", mTrendModel.getId());
        CallServer.getInstance().add(this, request, new HttpListener<String>() {

            @Override
            public void onSucceed(int what, Response<String> response) {
                JsonObject jsonObj = new JsonParser().parse(response.get()).getAsJsonObject();
                Boolean isSuccess = jsonObj.get("success").getAsBoolean();
                if (isSuccess) {
                    ToastUtil.makeText("删除成功");
                    finish();
                } else {
                    ToastUtil.makeText("删除失败");
                }
            }

            @Override
            public void onFailed(int what, Response<String> response) {
                ToastUtil.makeText("删除失败");
            }
        });
    }
}